package com.tensquare.recruit.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tensquare.recruit.dao.EnterpriseDao;
import com.tensquare.recruit.pojo.Enterprise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import java.util.List;

/**
 * @Description
 * @Date 2021/3/19 13:39
 */
@Service
@Transactional
public class EnterpriseService {
    @Autowired
    private EnterpriseDao enterpriseDao;

    @Autowired
    private IdWorker idWorker;

    public List<Enterprise> findAll() {
        return enterpriseDao.list();
    }

    public Enterprise findById(String enterpriseId) {
        return enterpriseDao.getById(enterpriseId);
    }

    public List<Enterprise> findByIshot() {
        return enterpriseDao.list(new QueryWrapper<Enterprise>().eq("ishot", 1));
    }

    public void save(Enterprise enterprise) {
        enterprise.setId(idWorker.nextId() + "");
        enterprise.setIshot("0");
        enterprise.setJobcount(0);
        enterpriseDao.save(enterprise);
    }

    public void update(Enterprise enterprise) {
        enterpriseDao.saveOrUpdate(enterprise);
    }

    public void deleteById(String enterpriseId) {
        enterpriseDao.removeById(enterpriseId);
    }

    public List<Enterprise> findSearch(Enterprise enterprise) {
        QueryWrapper<Enterprise> wrapper = new QueryWrapper();
        wrapper.like(StringUtils.isNotBlank(enterprise.getName()), "name", enterprise.getName());
        wrapper.like(StringUtils.isNotBlank(enterprise.getAddress()), "address", enterprise.getAddress());
        wrapper.like(StringUtils.isNotBlank(enterprise.getSummary()), "summary", enterprise.getSummary());
        wrapper.like(StringUtils.isNotBlank(enterprise.getLabels()), "labels", enterprise.getLabels());
        wrapper.like(StringUtils.isNotBlank(enterprise.getCoordinate()), "coordinate", enterprise.getCoordinate());
        return enterpriseDao.list(wrapper);
    }

    public Page<Enterprise> findSearchByPage(Enterprise enterprise, int page, int size) {
        QueryWrapper<Enterprise> wrapper = new QueryWrapper();
        wrapper.like(StringUtils.isNotBlank(enterprise.getName()), "name", enterprise.getName());
        wrapper.like(StringUtils.isNotBlank(enterprise.getAddress()), "address", enterprise.getAddress());
        wrapper.like(StringUtils.isNotBlank(enterprise.getSummary()), "summary", enterprise.getSummary());
        wrapper.like(StringUtils.isNotBlank(enterprise.getLabels()), "labels", enterprise.getLabels());
        wrapper.like(StringUtils.isNotBlank(enterprise.getCoordinate()), "coordinate", enterprise.getCoordinate());

        return null;
    }
}
